export const getYears = (startYear = 1984, endYear = 2022) => {
  // const startYear = 1984; // 2001  1983 1990
  // const endYear = 2022;

  let row = 9 // 列数
  const q = endYear - startYear // 总共的时间
  let col = 1 // 列的差距
  let y = 0 // 余数

  col = Math.floor(q / row) //每列宽度
  y = q % row // 余数

  if (y) {
    row += 1
  }
  let yearArr = []
  if (endYear - startYear < row) {
    for (let i = startYear; i <= endYear; i++) {
      yearArr.push(i)
    }
  } else {
    let curYear = startYear
    for (let i = 0; i <= row; i++) {
      let year = startYear
      if (i !== 0) {
        if (i === row) {
          year = curYear + (y || col)
        } else {
          year = curYear + col
        }
      }
      curYear = year
      yearArr.push(curYear)
    }
  }

  return { yearArr, q }
}

export const linkNodeNew = (trendData, defaultSelect, startYear, endYear) => {
  
  if ((!startYear && startYear !== 0) || !trendData || !trendData.terms) return

  const nodes = []
  const links = []
  const selectedTerms = {}
  let cnt = 0
  let colorMap = {}
  let colorIndex = 0
  let colorArray = [
    "#b11219",
    "#e32f27",
    "#fa6c4c",
    "#fca082",
    "#fdd4c2",
    "#f6f6f6",
    "RGB(172,182,197)",
    "RGB(202,205,205)",
    "RGB(214,220,230)",
    "RGB(229,231,234)"
  ]
  let initYear = endYear
  for (const term of trendData.terms) {
    if (trendData.unchoosed) {
      if (cnt < defaultSelect) term.selected = true
      else term.selected = false
    }

    if (term.selected) {
      selectedTerms[term.t] = true
      colorMap[term.t] = colorArray[colorIndex]
      colorIndex++
      if (colorIndex >= 10) {
        colorIndex = 0
      }
    }
    cnt += 1
  }

  const freqYearMap = {}
  for (let i = startYear; i <= endYear; i++) {
    freqYearMap[i] = {}
    Object.keys(selectedTerms).forEach((key) => {
      const freq = trendData.term_freq_by_year[key]
      freqYearMap[i][key] = freq[i]
    })
  }

  const nodeMap = {}
  let nodeindex = 0

  const { yearArr, q } = getYears(startYear, endYear)
  yearArr.forEach((year, i) => {
    const curYear = year
    let termFreqMap = freqYearMap[year]
    if (!termFreqMap) return
    Object.keys(termFreqMap).forEach((term, index) => {
      if (!nodeMap[term]) {
        nodeMap[term] = []
      }
      let freq = termFreqMap[term] || 0
      if (i > 0) {
        const preYear = nodeMap[term][i - 1].year
        if (curYear - preYear > 1) {
          for (let i = preYear + 1; i < curYear; i++) {
            freq += freqYearMap[i][term] || 0
          }
        }
      }
      const label = trendData.term_to_label[term]

      let heat = freq

      if (i > 0) {
        const _aX = 3
        const preNode = nodeMap[term].slice(-1)[0]
        let preFreq = preNode.pubs || 0
        if (heat <= 0 && preFreq === 0) {
          preFreq = preNode.w || 0
        }
        heat = (heat + _aX * preFreq) / (1 + _aX)
      }

      const _aP = 0.5
      heat = Math.pow(heat, _aP)

      nodeMap[term].push({
        id: nodeindex++,
        name: label,
        term: term,
        pubs: freq,
        w: heat,
        total: q,
        pos: i,
        col: i,
        year: curYear
      })
      nodes.push({
        name: label,
        term: label || term,
        pubs: freq,
        color: colorMap[term],
        // w: Math.pow(freq, 0.5),
        w: heat,
        total: q,
        pos: i,
        col: i,
        year: curYear
      })
    })
  })

  for (let keyword in nodeMap) {
    for (let i = 0; i < nodeMap[keyword].length - 1; i++) {
      links.push({
        firsrt: i == 0,
        source: nodeMap[keyword][i].id,
        target: nodeMap[keyword][i + 1].id,
        w1: nodeMap[keyword][i].w,
        w2: nodeMap[keyword][i + 1].w,
        color: colorMap[keyword]
      })
    }
  }
  return { nodes, links, initYear, q, yearArr }
  // return [nodes, links, initYear];
}
